#!/usr/bin/env python
import boto
import boto.ec2
import time
import os

os.environ['AWS_ACCESS_KEY_ID'] = '<you key id>'
os.environ['AWS_SECRET_ACCESS_KEY'] = '<your access key>'

regions = boto.ec2.regions()
eu = regions[0] #eu-west-1
conn_eu = eu.connect()
rs = conn_eu.get_all_instances(instance_ids='<your instance id>')
reservation = rs[0]
instance = reservation.instances[0]
#start an existing instance
instance.start()
print instance.state
while (instance.state != 'running'):
    time.sleep(5)
    instance.update()
    print 'pending ...'
ip = instance.ip_address
host = instance.public_dns_name
print host

duplicityString = "duplicity --no-encryption --scp-command 'scp -o IdentityFile=jonathlt.pem' --sftp-command 'sftp -o IdentityFile=/home/jonathan/scripts/jonathlt.pem' /home/jonathan/backup "
duplicityString += "scp://ubuntu@"
duplicityString += host
duplicityString += "//home/ubuntu/"

#print duplicityString

#execute
os.system('cd /home/jonathan/scripts')
os.system(duplicityString)

#now log out
instance.stop()

